package com.daixu.xutil.demo;

import android.content.Context;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.TextView;

import com.daixu.xutil.demo.widget.swipebacklayout.SwipeBackLayout;
import com.daixu.xutil.demo.widget.swipebacklayout.app.SwipeBackActivity;

import org.xutils.x;

/**
 * Created by yangbo on 2016/1/28.
 * 需要在子activity的layout文件头部添加 android:fitsSystemWindows="true"
 */
public class BaseActivity extends SwipeBackActivity {
    private AppLoader mAppLoader;

    protected static final int REQUEST_CODE_1 = 0x00000010;

    protected static final int REQUEST_CODE_2 = 0x00000020;

    protected static final int REQUEST_CODE_3 = 0x00000030;

    protected Button sBackBtn;

    private static final int VIBRATE_DURATION = 20;

    private SwipeBackLayout mSwipeBackLayout;

    private int color = R.color.title_bg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        x.view().inject(this);
        setTheme(R.style.AppBaseTheme);
        mAppLoader = AppLoader.getInstance();
        mAppLoader.addActivity(this);

        mSwipeBackLayout = getSwipeBackLayout();
        mSwipeBackLayout.setEdgeTrackingEnabled(SwipeBackLayout.EDGE_LEFT);
        mSwipeBackLayout.setStatusBarTintColor(color);
        mSwipeBackLayout.addSwipeListener(new SwipeBackLayout.SwipeListener() {
            @Override
            public void onScrollStateChange(int state, float scrollPercent) {

            }

            @Override
            public void onEdgeTouch(int edgeFlag) {
                vibrate(VIBRATE_DURATION);
            }

            @Override
            public void onScrollOverThreshold() {
                vibrate(VIBRATE_DURATION);
            }
        });
    }

    public void setStatusBarTintColor(int color) {
        this.color = color;
    }

    private int getStatusBarTintColor() {
        return color;
    }

    @Override
    protected void onDestroy() {
        // 将本对象从ActivityStack中移除
        mAppLoader.removeActivity(this);
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        restoreTrackingMode();
    }

    private void restoreTrackingMode() {
        mSwipeBackLayout.setEdgeTrackingEnabled(SwipeBackLayout.EDGE_LEFT);
    }

    private void vibrate(long duration) {
        Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        long[] pattern = {0, duration};
        vibrator.vibrate(pattern, -1);
    }

    /**
     * ****************************************************
     * 说明：快速设置标题名，标题组件的的id必须为text_title;
     *
     * @param text ******************************************************
     * @author gumengfu
     */
    protected void setTitleText(CharSequence text) {
        TextView textView = (TextView) findViewById(R.id.tv_title);
        if (textView != null) {
            textView.setText(text);
        }
    }

    /**
     * ****************************************************
     * 说明：快速设置标题名，标题组件的的id必须为text_title;
     *
     * @param resId ******************************************************
     * @author gumengfu
     */
    protected void setTitleText(int resId) {
        TextView textView = (TextView) findViewById(R.id.tv_title);
        if (textView != null) {
            textView.setText(resId);
        }
    }

    protected void showBackButton() {
        sBackBtn = (Button) findViewById(R.id.btn_back);
        if (sBackBtn != null)
            sBackBtn.setVisibility(View.VISIBLE);
    }

    public void doBack(View v) {
        super.onBackPressed();
        //finish();
    }

    @Override
    public void finish() {
        // 如果存在软键盘，关闭软键盘
        hideInputMethod();
        super.finish();
    }

    protected void hideInputMethod() {
        InputMethodManager imm = ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE));
        if (getCurrentFocus() != null && imm.isActive()) {
            imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    /**
     * ****************************************************
     * 说明：显示网络错误的对话框
     *
     * @author gumengfu
     * *****************************************************
     */
    protected void showNetworkErrorDialog() {
//        CustomDialog dialog = new CustomDialog.Builder(AppLoader.getInstance().currentActivity()).setTitle(R.string.network_error_title)
//                .setMessage(R.string.network_error_msg).setPositiveButton(R.string.setting_network, new DialogInterface.OnClickListener() {
//
//                    @Override
//                    public void onClick(DialogInterface dialog, int position) {
//                        dialog.dismiss();
//                        Intent wifiSetting = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);
//                        startActivity(wifiSetting);
//                    }
//                }).setNegativeButton(R.string.cancel, null).create();
//        dialog.show();
    }
}
